// pages/myCoupons/myCoupons.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    type:1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      navH: app.globalData.navHeight,
      time:new Date()
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    wx.request({
      url: app.globalData.url +'getUserCouponsList',
      method: 'GET',
      data:{
        userId:wx.getStorageSync("userId"),
        type : 1
      },
      success: function (res) {
        var data = res.data;
        console.log(data);
        if (data.result == 1 && data.data != undefined) {
          for (var i = 0; i < data.data.length; i++) {
            var d = new Date(data.data[i].validTime);
            var validTime = d.getFullYear() + '.' + (d.getMonth() + 1) + '.' + d.getDate() + ' ' + d.getHours() + ':' + d.getMinutes();
            data.data[i].validTime = validTime;
            var d = new Date(data.data[i].expireTime);
            var expireTime = d.getFullYear() + '.' + (d.getMonth() + 1) + '.' + d.getDate() + ' ' + d.getHours() + ':' + d.getMinutes();
            data.data[i].expireTime = expireTime;
          }
        }
        that.setData({
          couponsList: res.data.data
        });
      },
    });
  },
  // 点击tab切换view
  city: function (e) {
    var userid = wx.getStorageSync("userId");
    var that = this;
    if (this.data.type === e.target.dataset.type) {
      return false;
    } else {
      that.setData({
        type: e.target.dataset.type,
      })
    }
    wx.request({
      url: app.globalData.url + 'getUserCouponsList',
      method: 'GET',
      data: {
        userId: wx.getStorageSync("userId"),
        type: that.data.type
      },
      success: function (res) {
        var data = res.data;
        console.log(data);
        if (data.result == 1 && data.data != undefined) {
          for (var i = 0; i < data.data.length; i++) {
            var d = new Date(data.data[i].validTime);
            var validTime = d.getFullYear() + '.' + (d.getMonth() + 1) + '.' + d.getDate() + ' ' + d.getHours() + ':' + d.getMinutes();
            data.data[i].validTime = validTime;
            var d = new Date(data.data[i].expireTime);
            var expireTime = d.getFullYear() + '.' + (d.getMonth() + 1) + '.' + d.getDate() + ' ' + d.getHours() + ':' + d.getMinutes();
            data.data[i].expireTime = expireTime;
            if(d<that.data.time){
              data.data[i].valid = false;
            }
          }
        }
        that.setData({
          couponsList: res.data.data
        });
      },
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  navBack: function () {
    wx.navigateBack({

    })
  }
})